const ProjectModel = require('../model/projectModel')

const explorationService = {
    explorationfind: (userid)=>{
        console.log(userid)
        if(userid){
            return ProjectModel.ExplorationModel.find({userid})
            .then(data=>{
                return data
            })
        }else{
            return ProjectModel.ExplorationModel.find()
            .then(data=>{
                return data
            })
        }
    },

    explorationupdata: (body,query)=>{
        return ProjectModel.ExplorationModel.updateOne({id:query.id},{$set:{comments:body}})
        .then(data=>{
            console.log(data)
        })
    },

    explorationdelete: (query)=>{
        return ProjectModel.ExplorationModel.deleteOne({id:query.id})
        .then(data=>{
            console.log(data)
        })
    },

    explorationadd: async (body,file)=>{
        let id = await ProjectModel.ExplorationModel.find().sort({id:1})
        .then(data=>{
            return data[data.length-1].id+1
        })
        console.log(file.filename)
        let obj = {
            id: id,
            username: body.username,
            title: body.title,
            userid: body.userid,
            picture: ['http://localhost:3000/cities/img?img='+file.filename],
            centent: body.centent,
            comments: []
        }   
        return ProjectModel.ExplorationModel.create(obj)
        .then(data=>{
            return data
        })
    }
}

module.exports = explorationService